package com.bw.zg3_day03lx

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bw.base.entity.NoticeBeanItem
import com.bw.base.net.ApiService

class NoticeDataSource(val apiService: ApiService):PagingSource<Int, NoticeBeanItem>() {
    override fun getRefreshKey(state: PagingState<Int, NoticeBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NoticeBeanItem> {
        try {
            val currentPage = params.key?:0
            val notice = apiService.getNotice(currentPage, 10)
            val nextPage = if(notice.data.size == 10) currentPage + 1 else null
            return LoadResult.Page(notice.data,null, nextPage)
        }catch (e: Exception){
            return LoadResult.Error(e)
        }
    }
}